package com.android.base.util;

import org.json.JSONException;
import org.json.JSONObject;

public class FastJsonUtil {
    private JSONObject obj = null;
    private final String response;

    public FastJsonUtil(String resp) {
        response = resp;
    }

    private JSONObject getObject() throws JSONException {
        if (obj == null) {
            obj = new JSONObject(response);
        }
        return obj;
    }

    /**
     * 解析单个Key
     *
     * @param key
     * @return
     */
    public String parsingKey(String key) {
        String value = "";
        try {
            JSONObject jsonObject = getObject();
            value = jsonObject.getString(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return value;
    }

    /**
     * 解析单个Key
     *
     * @param key
     * @return
     */
    public int parsKey(String key) {
        int code = 0;
        try {
            JSONObject jsonObject = getObject();
            code = jsonObject.getInt(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 解析单个Key
     *
     * @param key
     * @return
     */
    public Double parsKeyDouble(String key) {
        double code = 0.0;
        try {
            JSONObject jsonObject = getObject();
            code = jsonObject.getDouble(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return code;
    }

}
